package com.mtons.mblog.modules.service.impl;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.modules.mapper.UserMapper;
import com.mtons.mblog.modules.pojo.UserFollowRel;
import com.mtons.mblog.modules.service.UserFollowService;
import com.mtons.mblog.modules.service.UserService;
import com.mtons.mblog.modules.service.helper.RedisFollowHelper;
import com.mtons.mblog.modules.vo.AccountProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserFollowServiceImpl
 * @Auther: Jerry
 * @Date: 2020/5/21 14:36
 * @Desctiption: TODO
 * @Version: 1.0
 */
@Service
public class UserFollowServiceImpl implements UserFollowService {

    private Logger log = LoggerFactory.getLogger(UserFollowServiceImpl.class);

    @Autowired
    private RedisFollowHelper redisFollowHelper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean isFollowed(Long followId, AccountProfile profile) {
        return redisFollowHelper.isFollowed(followId, profile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void follow(Long followId, AccountProfile profile) {
        userMapper.updateFans(followId, Consts.IDENTITY_STEP);
        redisFollowHelper.follow(followId, profile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unFollow(Long followId, AccountProfile profile) {
        userMapper.updateFans(followId, Consts.DECREASE_STEP);
        redisFollowHelper.unFollow(followId, profile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  List<UserFollowRel> myFollows(long userId){
        return getFriendList(userId,redisFollowHelper.myFollows(userId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserFollowRel> myFans(long followId) {
        return getFriendList(followId,redisFollowHelper.myFans(followId));
    }

    private boolean isFollowedByFollowId(Long followId, AccountProfile profile) {
        return redisFollowHelper.isFollowedByFollowId(followId, profile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  Long  myFollowCount(Long targetUserId) {
        Long count = redisFollowHelper.myFollowCount(targetUserId);
        return  count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  Long myFansCount(Long targetUserId) {
        Long count = redisFollowHelper.myFansCount(targetUserId);
        return  count;
    }

    /**
     * 获得用户关注或者粉丝列表
     * @param currUserId 当前用户
     * @param objectMap  调用方法
     * @return
     */
    private List<UserFollowRel> getFriendList(Long currUserId, Map<Object,Object> objectMap){
        List<UserFollowRel> followUserRespList = new ArrayList<UserFollowRel>();
        for (Map.Entry<Object, Object> entity :
                objectMap.entrySet()) {
            Object key = entity.getKey();
            Long id = Long.valueOf(String.valueOf(key));
            AccountProfile profile = userService.findProfile(id);
            UserFollowRel userFollowRel = new UserFollowRel();
            userFollowRel.setId(profile.getId());
            userFollowRel.setName(profile.getName());
            userFollowRel.setAvatar(profile.getAvatar());
            userFollowRel.setGender(profile.getGender());
            if (isFollowedByFollowId(currUserId, profile)) {
                userFollowRel.setFollowed(true);
            } else{
                userFollowRel.setFollowed(false);}
            followUserRespList.add(userFollowRel);
        }
        return followUserRespList;
    }

}
